package com.kangtu.demoweixinpay.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kangtu.demoweixinpay.httpclient.WechatPayHttpClientBuilder;
import com.kangtu.demoweixinpay.httpclient.auth.AutoUpdateCertificatesVerifier;
import com.kangtu.demoweixinpay.httpclient.auth.PrivateKeySigner;
import com.kangtu.demoweixinpay.httpclient.auth.WechatPay2Credentials;
import com.kangtu.demoweixinpay.httpclient.auth.WechatPay2Validator;
import com.kangtu.demoweixinpay.httpclient.util.PemUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.PrivateKey;
import java.util.HashMap;

/**
 * @program: demo-weix-pay
 * @description: PayController
 * @author: 亮子说编程
 * @create: 2021-03-01 15:17
 **/
@RestController
@Slf4j
@RequestMapping(value = "/weixin")
public class PayController {

    @Autowired
    RestTemplate restTemplate;

//    @Autowired
//    CloseableHttpClient closeableHttpClient;

    @GetMapping(value = "/pay")
    public String pay() {
//        log.info(closeableHttpClient.toString());
        return "hello,pay";
    }

    /**
     * @Description: doWeiXinLogin
     * @Param: [code]
     * @return: java.lang.String
     * @Author: 亮子
     * @Date: 2021/3/4 16:40
     *
     * 参考文档： https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
     */
    @PostMapping(value = "/doWeiXinLogin")
    public String doWeiXinLogin(String code) {
        String openId = "";
        String appId = "wx30819f332faafd24";
        String secret = "1a5e407c40668615c485b2a2865207b0";
        String url = String.format("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code",
                appId, secret, code);
        log.info(url);
        // https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
        openId = restTemplate.getForObject(url, String.class);
        log.info("open: "+openId);
        return openId;
    }

    @PostMapping(value = "/onLogin")
    public String onLogin(String code, HttpServletRequest request) {
        log.info("/onLogin:"+code);
        System.out.println(request.getParameterNames().toString());
        return code;
    }

    /**
    * @Description: 获取微信小程序的accesss_token
    * @Param: []
    * @return: java.lang.String
    * @Author: 亮子
    * @Date: 2021/3/2 11:42
    */
    @GetMapping(value = "/getAccessToken")
    public String getAccessToken() {
        // doc:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
        String res = "";
        String appId = "wx30819f332faafd24";
        String secret = "1a5e407c40668615c485b2a2865207b0";
        String url = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",
                appId, secret);
        log.info(url);
        // https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
        res = restTemplate.getForObject(url, String.class);
        return res;
    }

    @GetMapping(value = "/getLiveRooms")
    public String getLiveRooms() throws JsonProcessingException {
        String accessToken = "42_mz9T5gG3poc5kHx-233LxPjwNSpB64SSnBBTf_JcTIuxjesVHun3hyT1GUsVF_GsWx1KBkGfJ6Pbzx045aaG8QsZ2bNBdzHmwkJgCSXIjAiuBKlLrGbH_CdWmmNYWTvemMWHle0sziSy1YlXVHXcAIAZLB";
        String url = String.format("https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=%s",
                accessToken);
        log.info(url);
        // https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        ObjectMapper mapper = new ObjectMapper();
        HashMap<String, Object> map = new HashMap<>();
        map.put("start", 0);
        map.put("limit", 10);
        String params = mapper.writeValueAsString(map);

        HttpEntity<String> request = new HttpEntity<String>(params, headers);
        String res = restTemplate.postForObject(url, request, String.class);

        return res;
    }
}
